//
//  NetWork.m
//  UI18_AFNetWork_封装
//
//  Created by dllo on 15/8/31.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "NetWork.h"
#import <UIKit/UIKit.h>
#import "Reachability.h"
#import "AFNetworking.h"
#import "HUDManager.h"
#import "SVProgressHUD.h"

@interface NetWork ()<UIAlertViewDelegate>

@end
@implementation NetWork


+ (void)netWorkGETRequestWithURLAndNoMub:(NSString *)url Paramater:(NSDictionary *)dic ResultBlock:(void (^)(id))block
{
    if ([self isNetWorkConnectAvailiable]) {
        
        // 创建一个请求对象
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        // 请求https
        manager.securityPolicy.allowInvalidCertificates = YES;
        // 设置请求格式
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        // 设置响应格式
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        // 响应的类型
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/plain",@"application/x-javascript", nil]];
        
        [manager GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            // 转成JSON
            // 请求过来真正的二进制文件
            id result = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
            block(result);
            
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            NSLog(@"%@", error);
            // 可以展示一张图片
        }];
        
    }else{
        

        
    }
    
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
        NSLog(@"无网络");
    }else
        NSLog(@"取消");
}
// get无body
+ (void)netWorkGETRequestWithURL:(NSString *)url Paramater:(NSDictionary *)dic ResultBlock:(void (^)(id result))block
{
    if ([self isNetWorkConnectAvailiable]) {
        
        // 创建一个请求对象
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        // 请求https
        manager.securityPolicy.allowInvalidCertificates = YES;
        // 设置请求格式
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        // 设置响应格式
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        // 响应的类型
       [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/plain",@"application/x-javascript", nil]];
        // 加载指示器
        [HUDManager showStatus];
        // GET请求
        // 参数1: url
        // 参数2: 拼接的body
        // 参数3: success块
        // 参数4: fail块
        
        
        [manager GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
           
            // 转成JSON
            // 请求过来真正的二进制文件
            id result = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
            
            [HUDManager dismissHUD];
            block(result);
           
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            NSLog(@"------%@------", error);
            // 可以展示一张图片
        }];
        
    }else{
        
        
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无网络连接, 是否重新检查线路" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//        alert.delegate = self;
//        [alert show];
//        [alert release];
        
        
    }
}

/*
 * @brief 判断网络状态
 * @return YES 有网, NO无网
 
 */
+ (BOOL)isNetWorkConnectAvailiable
{
    BOOL isNet = YES;
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            
        {
            isNet = NO;
            NSLog(@"无网络连接");
            break;
        }
            
        case ReachableViaWiFi:
        {
            isNet = YES;
            NSLog(@"WIFI连接");
            break;
        }
        case  ReachableViaWWAN:
        {
            isNet = YES;
            NSLog(@"WWAN连接");
            break;
        }
        default:
            break;
    }
    return isNet;
}

// post有body
+ (void)netWorkPOSTRequestWithURL:(NSString *)url Body:(NSString *)bodyString Paramater:(NSDictionary *)dic ResultBlock:(void (^)(id))block
{
    if ([self isNetWorkConnectAvailiable]) {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy.allowInvalidCertificates = YES;
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/plain",@"application/x-javascript", nil]];
        [HUDManager showStatus];
        
        NSArray *bodyArr = [bodyString componentsSeparatedByString:@"&"];
        NSMutableDictionary *bodyDic = [NSMutableDictionary dictionary];
        for (NSString *string in bodyArr) {
            NSArray *tempArr = [string componentsSeparatedByString:@"="];
            [bodyDic setObject:tempArr[1] forKey:tempArr	[0]];
        }
        
        [manager POST:url parameters:bodyDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            id result = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:NSJSONReadingMutableContainers error:nil];
            [HUDManager dismissHUD];
            block(result);
          
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@", error);
        }];
    } else {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无网络连接, 是否重新检查线路" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
//        alert.delegate = self;
//        [alert release];
        
    }

    
}

// post有无body
+ (void)netWorkPOSTRequestWithURL:(NSString *)url Paramater:(NSDictionary *)dic ResultBlock:(void (^)(id))block
{
    if ([self isNetWorkConnectAvailiable]) {
        
        // 创建一个请求对象
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        // 请求https
        manager.securityPolicy.allowInvalidCertificates = YES;
        // 设置请求格式
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        // 设置响应格式
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        // 响应的类型
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/plain",@"application/x-javascript", nil]];
        // 加载指示器
        [HUDManager showStatus];
        // POST请求
        // 参数1: url
        // 参数2: 拼接的body
        // 参数3: success块
        // 参数4: fail块
        
        
        [manager POST:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            // 转成JSON
            // 请求过来真正的二进制文件
            id result = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
            
            [HUDManager dismissHUD];
            block(result);
           
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            NSLog(@"%@", error);
            // 可以展示一张图片
        }];
        
    }else{
        
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无网络连接" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//        [alert show];
//        alert.delegate = self;
//        [alert release];
        
    }

}

// get有body
+ (void)netWorkGETRequestWithURL:(NSString *)url Body:(NSString *)bodyString Paramater:(NSDictionary *)dic ResultBlock:(void (^)(id))block
{
    if ([self isNetWorkConnectAvailiable]) {
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        manager.securityPolicy.allowInvalidCertificates = YES;
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/plain",@"application/x-javascript", nil]];
        [HUDManager showStatus];
        
        NSArray *bodyArr = [bodyString componentsSeparatedByString:@"&"];
        NSMutableDictionary *bodyDic = [NSMutableDictionary dictionary];
        for (NSString *string in bodyArr) {
            NSArray *tempArr = [string componentsSeparatedByString:@"="];
            [bodyDic setObject:tempArr[1] forKey:tempArr	[0]];
        }
        
        [manager GET:url parameters:bodyDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            id result = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:NSJSONReadingMutableContainers error:nil];
            
            [HUDManager dismissHUD];
            block(result);
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@", error);
        }];
    } else {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无网络连接" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//        [alert show];
//        alert.delegate = self;
//        [alert release];
    }
    

}

+ (void)netWorkGETRequestWithURL:(NSString *)url Paramater:(NSDictionary *)dic IndexPath:(NSString *)path ResultBlock:(void (^)(id))block
{
    if ([self isNetWorkConnectAvailiable]) {
        
        // 创建一个请求对象
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        // 请求https
        manager.securityPolicy.allowInvalidCertificates = YES;
        // 设置请求格式
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        // 设置响应格式
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        // 响应的类型
        [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/css", @"text/plain",@"application/x-javascript", nil]];
        // 加载指示器
        [HUDManager showStatus];
        // GET请求
        // 参数1: url
        // 参数2: 拼接的body
        // 参数3: success块
        // 参数4: fail块
        
        
        [manager GET:url parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            // 转成JSON
            // 请求过来真正的二进制文件
            id result = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
            
            [result writeToFile:path atomically:YES];
            block(result);
            [HUDManager dismissHUD];
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
            NSLog(@"------%@------", error);
            // 可以展示一张图片
        }];
        
    }else{
        
    // 从缓存中读数据
    
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"无网络链接从缓存中读取数据" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alter show];
        id result = [NSData dataWithContentsOfFile:path];
        block(result);
        
        
    }

}
@end
